package personresources.manager.logic.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import personresources.manager.dao.EmployeeDao;
import personresources.manager.dto.EmployeeDto;
import personresources.manager.hibernate.Employee;
import personresources.manager.logic.EmployeeLogic;

@Service("employeeLogic")
public class EmployeeLogicImpl implements EmployeeLogic {

	@Autowired
	private EmployeeDao employeeDao;
	
	/**
	 * @see personresources.manager.logic.EmployeeLogic#getEmployeeByEmployeeId(java.lang.Integer)
	 */
	public Employee getEmployeeByEmployeeId(Integer employeeId) {
		return  employeeDao.read(employeeId);
	}

	/**
	 * @see personresources.manager.logic.EmployeeLogic#getEmployeeList(java.lang.Integer)
	 */
	public List<EmployeeDto> getEmployeeList(Integer groupId) {
		List<Employee> employeeList = employeeDao.getEmployees(groupId);
		List<EmployeeDto> employeeDtoList = new ArrayList<EmployeeDto>();
		int count = 1;
		for (Employee employee : employeeList) {
			EmployeeDto employeeDto = new EmployeeDto(employee);
			employeeDto.setIdCheck("check_" + count ++);
			employeeDtoList.add(employeeDto);
		}
		return employeeDtoList;
	}

	/**
	 * @see personresources.manager.logic.EmployeeLogic#getEmployeeByEmail(java.lang.String)
	 */
	public Employee getEmployeeByEmail(String email) {
		return (Employee)employeeDao.getEmployeeEmail(email);
	}

	/**
	 * @see personresources.manager.logic.EmployeeLogic#setEmployee(personresources.manager.hibernate.Employee)
	 */
	
	@Transactional(isolation=Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly=false, rollbackFor=Throwable.class)
	public void setEmployee(Employee employee) {
		employeeDao.create(employee);
//		Integer i = null;
//		i.intValue();
	}

}
